﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using ShopOnline.BLL;

namespace WebUI.Controls
{
    [Serializable]
    public partial class PaperListCtrl : BaseListControl, IListControl
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        public event ProductNameClickEventHandler ProductNameClick;

        protected void OnProductNameClick(EventArgs e)
        {
            if (ProductNameClick != null)
                ProductNameClick(this, e);
        }

        public CardPapers PaperList
        {
            get { return (CardPapers)SourceObject; }
            set { SourceObject = value; }
        }

        public Decimal GetTotal()
        {
            if (SourceObject == null)
                return 0;
            SourceObject.Rewind();
            decimal total = 0;
            for (int i = 0; i < SourceObject.RowCount; i++)
            {
                total += (int)SourceObject.GetColumn(Products.ColumnNames.Quantity) * (decimal)SourceObject.GetColumn(Products.ColumnNames.Cost);
                SourceObject.MoveNext();
            }

            return total;
        }

        bool allowViewDetail = false;

        public bool AllowViewDetails
        {
            get { return allowViewDetail; }
            set { allowViewDetail = value; }
        }

        protected void lnkProductName_OnClick(object sender, EventArgs e)
        {
            OnProductNameClick(e);
        }

        #region IListControl Members

        public void DeleteCheckedItems()
        {
            String checkedItemID = "";
            for (int i = 0; i < grid.Items.Count; i++)
            {
                DataGridItem item = grid.Items[i];
                Control ctrl = item.FindControl("cbSelect");
                if (ctrl != null && ctrl is CheckBox && ((CheckBox)ctrl).Checked && ((CheckBox)ctrl).Attributes["ItemID"] != null)
                    checkedItemID += ((CheckBox)ctrl).Attributes["ItemID"].ToString() + ",";
            }
            if (checkedItemID != "")
            {
                checkedItemID = checkedItemID.Remove(checkedItemID.Length - 1);

                CardPapers paper = new CardPapers();
                paper.Where.ID.Value = checkedItemID;
                paper.Where.ID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.In;
                paper.Query.Load();

                for (int i = 0; i < paper.RowCount; i++)
                {
                    paper.Deleted = true;
                    paper.MoveNext();
                }

                paper.Save();
            }
        }

        public void DeleteSelectedItem()
        {
            throw new NotImplementedException();
        }

        public void AddNewItem(MyGeneration.dOOdads.SqlClientEntity newItem)
        {
            throw new NotImplementedException();
        }

        public void UpdateItem(MyGeneration.dOOdads.SqlClientEntity updatedItem)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}